package tsm.network;

import java.io.ByteArrayOutputStream;
import java.util.Vector;

import tsm.constant.ClientConstant;
import tsm.main.Core;
import tsm.widget.ProgressPage;

public class BeUSession {
	public static final int USER_COMMAND = 0;
	public static final int CHAT_COMMAND = 1;
	public static final int FRIEND_COMMAND = 2;
	public static final int FILE_COMMAND = 3;
	public static final int MAGIC_COMMAND = 4;

	private static String username;

	private static String password;

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public static void requestIpList(byte language) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_IP_LIST_BEAN, USER_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeByte(language, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void clientInfo(int partnerId, String device, int appId, String version, int screenW, int screenH) {
		Message msg = Message.createMessageToSend(ClientConstant.CLIENT_INFO_BEAN, USER_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(partnerId, buffer);
		SerializeHelper.writeInt(appId, buffer);
		SerializeHelper.writeString(version, buffer);
		SerializeHelper.writeString(device, buffer);
		SerializeHelper.writeInt(screenW, buffer);
		SerializeHelper.writeInt(screenH, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void requestMagicPage(int pageId) {
		Core.getInstance().doConnect();
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_MAGIC_PAGE_BEAN, MAGIC_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(pageId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void uploadData(byte data[]) {
		Core.getInstance().doConnect();
		Message msg = Message.createMessageToSend(ClientConstant.MAGIC_PAGE_UPLOAD_BEAN, -1);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.write(data, 0, data.length, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void addBlackList(String userName) {
		Message msg = Message.createMessageToSend(ClientConstant.ADD_BLACK_LIST_BEAN, FRIEND_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(userName, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void removeBlackList(String userName) {
		Message msg = Message.createMessageToSend(ClientConstant.REMOVE_BLACK_LIST_BEAN, FRIEND_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(userName, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void requestAddFriend(String friendName, String[] groupName) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_ADD_FRIEND_BEAN, FRIEND_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(friendName, buffer);
		if (groupName != null) {
			int n = groupName.length;
			SerializeHelper.writeInt(n, buffer);
			for (int i = 0; i < n; i++) {
				SerializeHelper.writeString(groupName[i], buffer);
			}
		} else {
			SerializeHelper.writeInt(-1, buffer);
		}
		NetworkCore.sendMessage(msg);
	}

	public static void removeFriend(String userName) {
		Message msg = Message.createMessageToSend(ClientConstant.REMOVE_FRIEND_BEAN, FRIEND_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(userName, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void requestChangeGroup(String friendName, String[] groupName) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_CHANGE_GROUP_BEAN, FRIEND_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(friendName, buffer);
		if (groupName != null) {
			int n = groupName.length;
			SerializeHelper.writeInt(n, buffer);
			for (int i = 0; i < n; i++) {
				SerializeHelper.writeString(groupName[i], buffer);
			}
		} else {
			SerializeHelper.writeInt(-1, buffer);
		}
		NetworkCore.sendMessage(msg);
	}

	public static void requestFriendList(int checkSum) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_FRIEND_LIST_BEAN, FRIEND_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(checkSum, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void requestChangeStatus() {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_CHANGE_STATUS_BEAN, FRIEND_COMMAND);
		NetworkCore.sendMessage(msg);
	}

	public static void requestBlackList() {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_BLACKLIST_BEAN, FRIEND_COMMAND);
		NetworkCore.sendMessage(msg);
	}

	public static void sendChat(long to, String text) {
		Message msg = Message.createMessageToSend(ClientConstant.CHAT_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(to, buffer);
		SerializeHelper.writeString(text, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void sendChat(String to, String text) {
		Message msg = Message.createMessageToSend(ClientConstant.HANDSHAKE_CHAT_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(to, buffer);
		SerializeHelper.writeString(text, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void sendBuzz(String to) {
		Message msg = Message.createMessageToSend(ClientConstant.BUZZ_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(to, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void queryUserId(String username) {
		Message msg = Message.createMessageToSend(ClientConstant.GET_ID_BY_USERNAME_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(username, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void createConference() {
		Message msg = Message.createMessageToSend(ClientConstant.CONFERENCE_CREATE_BEAN, CHAT_COMMAND);
		//		ByteArrayOutputStream buffer = msg.getOutputStream();
		NetworkCore.sendMessage(msg);
	}

	public static void inviteMembers(long conferenceId, boolean isStartConference, Vector members) {
		Message msg = Message.createMessageToSend(ClientConstant.CONFERENCE_INVITE_MEMBERS_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(conferenceId, buffer);
		SerializeHelper.writeBoolean(isStartConference, buffer);
		int n = members.size();
		SerializeHelper.writeInt(n, buffer);
		if (n > 0) {
			for (int i = 0; i < n; i++) {
				SerializeHelper.writeString(members.elementAt(i).toString(), buffer);
			}
		}
		NetworkCore.sendMessage(msg);
	}

	public static void sendChatConference(long conferenceId, String content) {
		Message msg = Message.createMessageToSend(ClientConstant.CONFERENCE_CHAT_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(conferenceId, buffer);
		SerializeHelper.writeString(Core.getInstance().username, buffer);
		SerializeHelper.writeString(content, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void leaveConference(long conferenceId) {
		Message msg = Message.createMessageToSend(ClientConstant.CONFERENCE_REQUEST_LEAVE_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(conferenceId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void callKickConferencePage(long conferenceId) {
		Message msg = Message.createMessageToSend(ClientConstant.CALL_KICK_CONFERENCE_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(conferenceId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void answerJoinConference(long conferenceId, boolean isAccepted) {
		Message msg = Message.createMessageToSend(ClientConstant.CONFERENCE_CONFIRM_JOIN_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(conferenceId, buffer);
		SerializeHelper.writeBoolean(isAccepted, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void getImagePage(int pageId, int componentId, int imageId) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_GET_IMAGE_PAGE_BEAN, MAGIC_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(pageId, buffer);
		SerializeHelper.writeInt(componentId, buffer);
		SerializeHelper.writeInt(imageId, buffer);
		NetworkCore.sendMessage(msg);
	}

	private static final int CHUNK_SIZE = 3 * 1024;

	private static byte[] uploadFile;

	public static long sessionId;

	public static long entityForUpload;

	public static long albumIdForUpload;

	public static int totalPart;

	public static void requestUploadImage(byte[] data) {
		//		System.out.println("+++ requestUploadImage data length = " + data.length);
		totalPart = data.length / CHUNK_SIZE;
		uploadFile = data;
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_UPLOAD_BEAN, FILE_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(data.length, buffer);
		SerializeHelper.writeLong(entityForUpload, buffer);
		SerializeHelper.writeLong(albumIdForUpload, buffer);
		NetworkCore.sendMessage(msg);
		Core.getInstance().createProgressPage();
	}

	public static void uploadChunk(int chunkId) {
		if (sessionId != -1) {
			int index = chunkId * CHUNK_SIZE;
			int length = CHUNK_SIZE;
			int fileLength = uploadFile.length;
			if (index + length > fileLength) {
				length = fileLength - index;
			}
			//		System.out.println("+++ uploadChunk index = " + index + " length = " + length + " chunkId " + chunkId + " fileLength " + fileLength);
			Message msg = Message.createMessageToSend(ClientConstant.CHUNK_FILE_BEAN, FILE_COMMAND);
			ByteArrayOutputStream buffer = msg.getOutputStream();
			SerializeHelper.writeLong(sessionId, buffer);
			SerializeHelper.writeInt(chunkId, buffer);
			SerializeHelper.write(uploadFile, index, length, buffer);
			NetworkCore.sendMessage(msg);
			ProgressPage.setProgressPercent((chunkId + 1) * 100 / totalPart);
		}
	}

	public static void cancelUploadImage() {
		Message msg = Message.createMessageToSend(ClientConstant.CANCEL_UPLOAD_IMAGE_BEAN, FILE_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(sessionId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void requestAvatarImage(int itemId, long userId) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_IMAGE_AVATAR_BEAN, USER_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(itemId, buffer);
		SerializeHelper.writeLong(userId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void requestLazyBuddyInfo(long userId) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_LAZY_BUDDY_INFO_BEAN, FRIEND_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(userId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void beginStreamingAudio(int songId) {
		Core.getInstance().currentSongId = songId;
		Message msg = Message.createMessageToSend(ClientConstant.BEGIN_STREAMING_AUDIO_BEAN, FILE_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(songId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void requestNextChunkAudio(int songId, int nextChunk) {
		Message msg = Message.createMessageToSend(ClientConstant.REQUEST_NEXT_CHUNK_AUDIO_BEAN, FILE_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeInt(songId, buffer);
		SerializeHelper.writeInt(nextChunk, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void getMEPage(long userId) {
		Message msg = Message.createMessageToSend(ClientConstant.GET_ME_PAGE_BEAN, USER_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeLong(userId, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void logoutYahoo() {
		Message msg = Message.createMessageToSend(ClientConstant.YAHOO_LOGOUT_BEAN, CHAT_COMMAND);
		NetworkCore.sendMessage(msg);
	}

	public static void setStatusYahooBean() {
		Message msg = Message.createMessageToSend(ClientConstant.YAHOO_SET_STATUS_BEAN, CHAT_COMMAND);
		NetworkCore.sendMessage(msg);
	}

	public static void chatYahoo(String ymName, String content) {
		Message msg = Message.createMessageToSend(ClientConstant.YAHOO_CHAT_TO_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(ymName, buffer);
		SerializeHelper.writeString(content, buffer);
		NetworkCore.sendMessage(msg);
	}

	public static void buzzYahoo(String ymName) {
		Message msg = Message.createMessageToSend(ClientConstant.YAHOO_BUZZ_TO_BEAN, CHAT_COMMAND);
		ByteArrayOutputStream buffer = msg.getOutputStream();
		SerializeHelper.writeString(ymName, buffer);
		NetworkCore.sendMessage(msg);
	}
}
